package org.liukai.ems.controller;

import org.liukai.ems.entity.po.User;
import org.liukai.ems.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @create UserController.java 17:06
 * 
 * @author liukai
 * @date 2015/12/17
 */
@Controller
//@SessionAttributes("currentUser")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //登录
    @RequestMapping("/login")
    public ModelAndView login(User user, HttpServletRequest request) {
        User u = userService.queryUserByUserName(user.getUsername());
        ModelAndView model = new ModelAndView("user/login");
        //model.addObject("user", user);
        if(null == u) {
            model.addObject("msg", "用户不存在");
            return model;
        } else if(!u.getPwd().equals(user.getPwd())) {
            model.addObject("msg", "密码错误");
            return model;
        }
        HttpSession session = request.getSession();
        session.setAttribute("currentUser", u);
        //model.addObject("currentUser", u);
        //session.setAttribute("user", 123);
        //model.addObject("currentUser", user);
        model = new ModelAndView("redirect:/emp/empList");
        return model;
    }

    //跳转到登陆页面
    @RequestMapping("/toLogin")
    public ModelAndView toLogin(HttpServletRequest request) {
        //System.out.println(request.getSession().getAttribute("currentUser"));
//        if(request.getSession().getAttribute("currentUser") != null) {
//            request.getSession().invalidate();
//        }
    	HttpSession session = request.getSession();
    	User user = (User)session.getAttribute("currentUser");
    	Object obj = session.getAttribute("user");
    	if(obj != null) {
    		System.out.println((Integer)obj);
    	}
    	if(user != null) {
    		System.out.println(user.getUsername());
    	}
        return new ModelAndView("user/login");
    }

    //登出
    @RequestMapping("/logout")
    public ModelAndView logout( HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        if(session == null) {
            System.out.println("session is null");
        }
        //System.out.println("3333333333333333333333333333333333333333333" + session.getId());
        //session.setAttribute("currentUser", null);
        session.removeAttribute("currentUser");
        session.removeAttribute("user");
        session.invalidate();
        //System.out.println(session.getId());
        //System.out.println(session.getCreationTime());
        //Object obj = session.getAttribute("currentUser");
        //session.removeAttribute("currentUser");
        //System.out.println("2222222222222222222222222222222222222222222" + session.getId());
        //response.sendRedirect("/user/toLogin");
        //return null;
        return new ModelAndView("redirect:/user/toLogin");
    }
}
